package cn.wit.summit.chwan.utils;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * User: mawanjin@join-cn.com
 * Date: 14-9-17
 * Time: 上午11:15
 */
public class DateUtils {

    /**
     * 下载日期是几分钟前、几天前
     *
     * @return
     */
    public static String FormatForDownloadTime(long begin) {
        long second = 1000;//1s=1000ms
        long minute = second * 60;
        long hour   = minute * 60;
        long day    = hour * 24;
        long month  = day * 30;
        long year   = month * 12;

        long now = System.currentTimeMillis();
        long gap = now - begin;
        if (gap < minute) return "刚刚"; //小于1分钟
        else if (gap < hour) return gap / minute + "分钟前";   //小于1小时
        else if (gap < day) return gap / hour + "小时前";//小于24小时
        else if (gap < month) return gap / day + "天前";  //1天前，1一个月以内
        else if (gap < year) return gap / month + "月前";   //1天前，1一个月以内
        else return gap / year + "年前";// 1年前

    }

    /**
     * 下载日期是几分钟前、几天前
     *
     * @return
     */
    public static String FormatForDownloadTimeDetail(long begin) {
        long second = 1000;//1s=1000ms
        long minute = second * 60;
        long hour   = minute * 60;
        long day    = hour * 24;
        long month  = day * 30;
        long year   = month * 12;

        long now = System.currentTimeMillis();
        long gap = now - begin;
        if (gap < minute) return "刚刚"; //小于1分钟
        else if (gap < hour) return gap / minute + "分钟前";   //小于1小时
        else if (gap < day) return gap / hour + "小时前";//小于24小时
        else if (gap < month) return gap / day + "天前";  //1天前，1一个月以内
        else if (gap < year) return gap / month + "月前";   //1天前，1一个月以内
        else return gap / year + "年前";// 1年前

    }

    /**
     * 下载日期是几分钟前、几天前
     *
     * @return
     */
    public static String TimeToNowFucher(long begin) {
        long second = 1000;//1s=1000ms
        long minute = second * 60;
        long hour   = minute * 60;
        long day    = hour * 24;
        long month  = day * 30;
        long year   = month * 12;

        long now = System.currentTimeMillis();
        long gap = begin - now;
        if (isToday(begin)) {
            return "今天";
        }
        if (gap < 0) {
            gap = now - begin;
            if (gap < minute) return "今天"; //小于1分钟
            else if (gap < hour) return "今天";   //小于1小时
            else if (gap < day) return "昨天";//小于24小时
            else if (gap < month) return gap / day + "天前";  //1天前，1一个月以内
            else if (gap < year) return gap / month + "月前";   //1天前，1一个月以内
            else return gap / year + "年前";// 1年前
        } else {
            if (gap < hour) return "今天";   //小于1小时
            else if (gap < day) return "明天";//小于24小时
            else if (gap < month) return gap / day + "天后";  //1天前，1一个月以内
            else if (gap < year) return gap / month + "月后";   //1天前，1一个月以内
            else return gap / year + "年后";// 1年前

        }
//        if (gap < minute) return ""; //小于1分钟

    }

    /**
     * 下载日期是几分钟前、几天前
     *
     * @return
     */
    public static String TimeToNowFucherDeail(long begin) {
        long second = 1000;//1s=1000ms
        long minute = second * 60;
        long hour   = minute * 60;
        long day    = hour * 24;
        long month  = day * 30;
        long year   = month * 12;

        long now = System.currentTimeMillis();
        long gap = begin - now;
        if (gap < 0) return "已开服";
//        if (gap < minute) return ""; //小于1分钟
        else if (gap < hour) return gap / minute + "天";   //小于1小时
        else if (gap < day) return gap / hour + "天";//小于24小时
        else if (gap < month) return gap / day + "天";  //1天前，1一个月以内
        else if (gap < year) return gap / month + "月";   //1天前，1一个月以内
        else return gap / year + "年";// 1年前

    }


    /**
     * 是否是今天
     *
     * @param time
     * @return
     */
    public static boolean isToday(long time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);

        int year  = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day   = calendar.get(Calendar.DATE);

        calendar.setTimeInMillis(System.currentTimeMillis());
        int nYear  = calendar.get(Calendar.YEAR);
        int nMonth = calendar.get(Calendar.MONTH);
        int nDay   = calendar.get(Calendar.DATE);

        return year == nYear && month == nMonth && day == nDay;


    }

    /**
     * 是否是今年
     *
     * @param time
     * @return
     */
    public static boolean isToYear(long time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);

        int year  = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day   = calendar.get(Calendar.DATE);

        calendar.setTimeInMillis(System.currentTimeMillis());
        int nYear  = calendar.get(Calendar.YEAR);
        int nMonth = calendar.get(Calendar.MONTH);
        int nDay   = calendar.get(Calendar.DATE);

        return year == nYear;


    }


    public static String generateTime(long time) {
        int totalSeconds = (int) (time / 1000);
        int seconds      = totalSeconds % 60;
        int minutes      = (totalSeconds / 60) % 60;
        int hours        = totalSeconds / 3600;

        return hours > 0 ? String.format("%02d:%02d:%02d", hours, minutes, seconds) : String.format("%02d:%02d", minutes, seconds);
    }

    /**
     * 把long 转换成 日期 再转换成String类型
     */
    public static String transferLongToDate(String dateFormat, Long millSec) {
        SimpleDateFormat sdf  = new SimpleDateFormat(dateFormat);
        Date             date = new Date(millSec);
        return sdf.format(date);
    }

    /**
     * 星期几
     *
     * @param time long 系统时间的long类型
     * @return 星期一到星期日
     */
    public static String getWeekOfDate(long time) {
        Date     date     = new Date(time);
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal      = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;

        return weekDays[w];
    }
    /**
     * 得到MM月dd日 HH:mm
     *
     * @return
     */
//    public static String getTimeYMDHM(long dateDate) {
////        dateDate = dateDate - 8 * 60 * 60 * 1000;
//        SimpleDateFormat formatter = new SimpleDateFormat("YY-MM-dd    HH:mm");
//        formatter.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
//        String dateString = formatter.format(dateDate);
//
//        return dateString;
//    }
    /**
     * 得到MM月dd日 HH:mm
     *
     * @return
     */
    public static String getTimeHM(long dateDate) {
//        dateDate = dateDate - 8 * 60 * 60 * 1000;
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
        formatter.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        String dateString = formatter.format(dateDate);

        return dateString;
    }
    /**
     * 得到MM月dd日 HH:mm
     *
     * @return
     */
    public static String getTimeMD(long dateDate) {
//        dateDate = dateDate - 8 * 60 * 60 * 1000;
        SimpleDateFormat formatter = new SimpleDateFormat("MM-dd");
        formatter.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        String dateString = formatter.format(dateDate);

        return dateString;
    }
    /**
     * 得到MM月dd日 HH:mm
     *
     * @return
     */
    public static String getTimeMDHM(long dateDate) {
//        dateDate = dateDate - 8 * 60 * 60 * 1000;
        SimpleDateFormat formatter = new SimpleDateFormat("MM-dd HH:MM");
        formatter.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        String dateString = formatter.format(dateDate);

        return dateString;
    }
    /**
     * 得到MM月dd日 HH:mm
     *
     * @return
     */
    public static String getTimeYMDHM(long dateDate) {
//        dateDate = dateDate - 8 * 60 * 60 * 1000;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd  HH:MM");
        formatter.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        String dateString = formatter.format(dateDate);

        return dateString;
    }

}
